package com.tbit.uqbike.service.business.impl;

import com.tbit.uqbike.object.pojo.RefundOpeLog;
import com.tbit.uqbike.object.pojo.statistics.ExportRefundOpeLog;
import com.tbit.uqbike.service.business.RefundOpeLogService;
import com.tbit.uqbike.webmanager.dao.core.AccountUserDao;
import com.tbit.uqbike.webmanager.dao.core.UserDao;
import com.tbit.uqbike.webmanager.dao.log.RefundOpeLogDao;
import com.tbit.uqbike.webmanager.dao.log.RideRefundLogDao;
import com.tbit.utils.DateTimeUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 退款记录(RefundOpeLog)表服务实现类
 *
 * @author jing
 * @since 2020-11-20 15:47:22
 */
@Service("RefundOpeLogService")
public class RefundOpeLogServiceImpl implements RefundOpeLogService {
    @Resource
    private RefundOpeLogDao RefundOpeLogDao;
    @Resource
    private AccountUserDao accountUserDao;
    @Resource
    private UserDao userDao;
    @Resource
    private RideRefundLogDao rideRefundLogDao;

    /**
     * 通过ID查询单条数据
     *
     * @param accountId 主键
     * @return 实例对象
     */
    @Override
    public RefundOpeLog queryById(Integer accountId) {
        return this.RefundOpeLogDao.queryById(accountId);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<RefundOpeLog> queryAllByLimit(int offset, int limit) {
        return this.RefundOpeLogDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param RefundOpeLog 实例对象
     * @return 实例对象
     */
    @Override
    public RefundOpeLog insert(RefundOpeLog RefundOpeLog) {
        this.RefundOpeLogDao.insert(RefundOpeLog);
        return RefundOpeLog;
    }

    /**
     * 修改数据
     *
     * @param RefundOpeLog 实例对象
     * @return 实例对象
     */
    @Override
    public RefundOpeLog update(RefundOpeLog RefundOpeLog) {
        this.RefundOpeLogDao.update(RefundOpeLog);
        return this.queryById(RefundOpeLog.getAccountId());
    }

    /**
     * 通过主键删除数据
     *
     * @param accountId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer accountId) {
        return this.RefundOpeLogDao.deleteById(accountId) > 0;
    }

    @Override
    public List<RefundOpeLog> queryPage(RefundOpeLog refundOpeLog) {
        refundOpeLog.setPageNO((refundOpeLog.getPageNO() - 1) * refundOpeLog.getRowCount());
        return RefundOpeLogDao.selectByRefundOpeLog(refundOpeLog);
    }

    @Override
    public Integer queryCount(RefundOpeLog RefundOpeLog) {
        return RefundOpeLogDao.selectByRefundOpeLogCount(RefundOpeLog);
    }

    @Override
    public List<ExportRefundOpeLog> export(RefundOpeLog RefundOpeLog) {
        List<ExportRefundOpeLog> exports = RefundOpeLogDao.export(RefundOpeLog);
        //影响性能，去除
        /*exports.stream().forEach(export -> {
            if (null != export.getUserId()) {
                export.setUserId(userDao.getByUserId(Integer.parseInt(export.getUserId())).getName());
            }
            if (null != export.getAccountUserId()) {
                export.setAccountUserId(accountUserDao.getAccountUserById(Integer.parseInt(export.getAccountUserId())).getName());
            }
        });*/
        return exports;
    }

    @Override
    public List<Map<String, Object>> queryByTime(RefundOpeLog RefundOpeLog) {
        List<Map<String, Object>> result = new ArrayList<>();
        /**获取当前时间段的每一天日期*/
        List<String> daysStr = DateTimeUtil.findDaysStr(DateTimeUtil.formatDateYMD(RefundOpeLog.getStartTime()), DateTimeUtil.formatDateYMD(RefundOpeLog.getEndTime()));
        daysStr.stream().forEach(days -> {
            RefundOpeLog.setTime(days);
            result.add(RefundOpeLogDao.queryByTime(RefundOpeLog));
        });
        return result;
    }


}